package com.ruoyi.ax.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.ax.domain.*;
import com.ruoyi.ax.mapper.AxOrderMapper;
import com.ruoyi.ax.mapper.AxRoundMapper;
import com.ruoyi.ax.service.IAxAuthorizationSellService;
import com.ruoyi.ax.service.IAxOrderService;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ax.mapper.AxGoodMapper;
import com.ruoyi.ax.service.IAxGoodService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class AxGoodServiceImpl implements IAxGoodService {
    @Autowired
    private AxGoodMapper axGoodMapper;

    @Autowired
    private AxRoundMapper axRoundMapper;

    @Autowired
    private IAxAuthorizationSellService iAxAuthorizationSellService;

    @Autowired
    private IAxOrderService axOrderService;

    @Autowired
    private AxOrderMapper axOrderMapper;

    /**
     * 查询商品管理
     *
     * @param id 商品管理主键
     * @return 商品管理
     */
    @Override
    public AxGood selectAxGoodById(Long id) {
        AxGood axgood = axGoodMapper.selectAxGoodById(id);
        if(axgood!=null){
            axgood.setAxRound(axRoundMapper.selectAxRoundById(axgood.getRoundId()));
        }else{
            axgood = new AxGood();
            axgood.setAxRound(new AxRound());
        }

        return axgood;
    }

    @Override
    @Transactional
    public AxGood   getAbleSelledInfo(Long goodId) {
//        AxAuthorizationSell axAuthorizationSell=new AxAuthorizationSell();
//        axAuthorizationSell.setSysUserId(SecurityUtils.getUserId());
//        axAuthorizationSell.setGoodId(goodId);
        AxOrder axOrder=new AxOrder();
        axOrder.setSysUserId(SecurityUtils.getUserId());
        axOrder.setOrderType(1l);
        axOrder.setGoodId(goodId);
       List<AxOrder>  list = axOrderService.selectAxOrderList(axOrder);
        long   ableSelledNum=0;
      //  List<AxAuthorizationSell> list = iAxAuthorizationSellService.selectAxAuthorizationSellWithOrderList(axAuthorizationSell);
        if(list.size()==0){
            throw  new GlobalException("您无此商品订单");
        }else{
            for(AxOrder axOrder1:list ){
                ableSelledNum=ableSelledNum+ axOrder1.getTotalNum();
            }
        }
        AxGood axGood = axGoodMapper.selectAxGoodById(goodId);
        axGood.setUserAbleSellNum(ableSelledNum);
        return axGood;
    }



    @Override
    @Transactional
    public int  sellGood(Long goodId) {
        AxOrder axOrder=new AxOrder();
        axOrder.setSysUserId(SecurityUtils.getUserId());
        axOrder.setOrderType(1l);
        axOrder.setGoodId(goodId);
        List<AxOrder>  list = axOrderService.selectAxOrderList(axOrder);
        if(list.size()==0){
            throw  new GlobalException("您无此商品订单");
        }else{
            for(AxOrder axOrder1:list ){
                AxAuthorizationSell axAuthorizationSell=new AxAuthorizationSell();
                axAuthorizationSell.setOrderId(axOrder1.getId());
                axOrderService.sellGood(axAuthorizationSell);
            }
        }
        return list.size();
    }

    /**
     * 查询商品管理列表
     *
     * @param axGood 商品管理
     * @return 商品管理
     */
    @Override
    public List<AxGood> selectAxGoodList(AxGood axGood) {

        List<AxGood> list =  axGoodMapper.selectAxGoodList(axGood);
        for(AxGood axGood1:list){
            axGood1.setAxRound(axRoundMapper.selectAxRoundById(axGood1.getRoundId()));
        }
        return list;
    }

    @Override
    public List<AxGood> selectPlatformBAxGoodList(AxGood axGood) {
        axGood.setEndYmd(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,new Date()));
        List<AxGood> list = new ArrayList<>();
        if(axGood.getGoodClassify()==null){
            list =  axGoodMapper.selectPlatformBAxGoodList(axGood);
        }else
        {
            list =  axGoodMapper.selectPlatformBWithAllowanceAxGoodList(axGood);
        }
        return list;
    }




    /**
     * 新增商品管理
     *
     * @param axGood 商品管理
     * @return 结果
     */
    @Override
    public int insertAxGood(AxGood axGood) {
        axGood.setCreateTime(DateUtils.getNowDate());
        return axGoodMapper.insertAxGood(axGood);
    }

    /**
     * 修改商品管理
     *
     * @param axGood 商品管理
     * @return 结果
     */
    @Override
    public int updateAxGood(AxGood axGood) {
        axGood.setUpdateTime(DateUtils.getNowDate());
        return axGoodMapper.updateAxGood(axGood);
    }

    /**
     * 批量删除商品管理
     *
     * @param ids 需要删除的商品管理主键
     * @return 结果
     */
    @Override
    public int deleteAxGoodByIds(Long[] ids) {

        for(long goodId:ids){
            AxOrder axOrder =new AxOrder();
            axOrder.setGoodId(goodId);
            int  orderCount= axOrderMapper.selectAxOrderCount(axOrder);
            if(orderCount>0){
                throw new GlobalException("该商品存在订单信息,不能删除.......");
            }
        }


        return axGoodMapper.deleteAxGoodByIds(ids);
    }

    /**
     * 删除商品管理信息
     *
     * @param id 商品管理主键
     * @return 结果
     */
    @Override
    public int deleteAxGoodById(Long id) {
        return axGoodMapper.deleteAxGoodById(id);
    }
}


